package com.test.platform.module.perf.controller;

import com.test.common.form.perf.ScriptModifyForm;
import com.test.common.response.ResponseResult;
import com.test.common.utils.FileUploadUtils;
import com.test.platform.module.perf.service.PerfScriptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Api(value = "value", tags = "性能平台脚本文件管理")
@RestController
@RequestMapping("/perf/script")
@ResponseResult
public class PerfScriptController {
    @Resource
    private PerfScriptService perfScriptService;

    @ApiOperation("上传脚本文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "__file", name = "file", value = "待上传的文件", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "uploadId", value = "用于记录进度的ID", required = true),
    })
    @PostMapping("/upload")
    public Object fileUpload(HttpServletRequest request, @RequestParam("uploadId") String uploadId) {
        return perfScriptService.fileUpload(FileUploadUtils.fileUpload(request, uploadId));
    }

    @ApiOperation("查询文件上传进度")
    @GetMapping("/upload/process")
    public Object uploadProcess(HttpServletRequest request, @RequestParam("uploadId") String uploadId) {
        return FileUploadUtils.uploadProcess(request, uploadId);
    }

    @ApiOperation("清理文件上传进度记录")
    @GetMapping("/clear/process")
    public Object clearProcess(HttpServletRequest request, @RequestParam("uploadId") String uploadId) {
        return FileUploadUtils.clearProcess(request, uploadId);
    }

    @ApiOperation("查询脚本文件内容")
    @GetMapping("/detail")
    public Object detail(@RequestParam("id") int id) {
        return perfScriptService.detail(id);
    }

    @ApiOperation("下载文件")
    @GetMapping("/download")
    public void download(@RequestParam("id") int id, HttpServletResponse response) {
        perfScriptService.download(id, response);
    }

    @ApiOperation("删除文件")
    @GetMapping("/delete")
    public Object delete(@RequestParam("id") int id) {
        return perfScriptService.delete(id);
    }

    @ApiOperation("查找所有脚本文件")
    @GetMapping("/find/all")
    public Object findAll(@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize) {
        return perfScriptService.findAll(pageNo, pageSize);
    }

    @GetMapping("/updateResource")
    public Object updateResource(int scriptId, int[] resourceIds) {
        return perfScriptService.updateResource(scriptId, resourceIds);
    }

    @PostMapping("/updateContent")
    public Object updateContent(@RequestBody ScriptModifyForm scriptModifyForm) {
        return perfScriptService.updateContent(scriptModifyForm);
    }
}
